home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / multiscreendoor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.9 KB  |  196 lines

  1. #include <assert.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <GL/glut.h>
  6.  
  7. /* Some <math.h> files do not define M_PI... */
  8. #ifndef M_PI
  9. #define M_PI 3.14159265358979323846
  10. #endif
  11.  
  12. #ifdef _WIN32
  13. #define trunc(x) ((double)((int)(x)))
  14. #define random() ((long)rand() + (rand() << 15) + (rand() << 30))
  15. #endif
  16.  
  17. GLUquadricObj *cone, *base, *qsphere;
  18.  
  19. GLuint conePattern[32], spherePattern[32];
  20.  
  21. void create_stipple_pattern(GLuint *pat, GLfloat opacity)
  22. {
  23.   int x, y;
  24.   long threshold = (float)0x7fffffff * (1. - opacity);
  25.  
  26.   for (y = 0; y < 32; y++) {
  27.     pat[y] = 0;
  28.     for (x = 0; x < 32; x++) {
  29.       if (random() > threshold) pat[y] |= (1 << x);
  30.     }
  31.   }
  32. }
  33.  
  34. void init(void)
  35. {
  36.   static GLfloat lightpos[] = {.5, .75, 1.5, 1};
  37.  
  38.   glEnable(GL_DEPTH_TEST); 
  39.   glEnable(GL_LIGHTING);
  40.   glEnable(GL_LIGHT0);
  41.  
  42.   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  43.  
  44.   cone = gluNewQuadric();
  45.   base = gluNewQuadric();
  46.   qsphere = gluNewQuadric();
  47.   gluQuadricOrientation(base, GLU_INSIDE);
  48.  
  49.   create_stipple_pattern(spherePattern, .5);
  50.   create_stipple_pattern(conePattern, .5);
  51. }
  52.  
  53. void reshape(GLsizei w, GLsizei h) 
  54. {
  55.   glViewport(0, 0, w, h);
  56.   
  57.   glMatrixMode(GL_PROJECTION);
  58.   glLoadIdentity();
  59.   gluPerspective(60, 1, .01, 10);
  60.   gluLookAt(0, 0, 2.577, 0, 0, -5, 0, 1, 0);
  61.   
  62.   glMatrixMode(GL_MODELVIEW);
  63.   glLoadIdentity();
  64. }
  65.  
  66. void draw_room(void)
  67. {
  68.   /* material for the walls, floor, ceiling */
  69.   static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f};
  70.  
  71.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
  72.  
  73.   glBegin(GL_QUADS);
  74.   
  75.   /* floor */
  76.   glNormal3f(0, 1, 0);
  77.   glVertex3f(-1, -1, 1);
  78.   glVertex3f(1, -1, 1);
  79.   glVertex3f(1, -1, -1);
  80.   glVertex3f(-1, -1, -1);
  81.  
  82.   /* ceiling */
  83.   glNormal3f(0, -1, 0);
  84.   glVertex3f(-1, 1, -1);
  85.   glVertex3f(1, 1, -1);
  86.   glVertex3f(1, 1, 1);
  87.   glVertex3f(-1, 1, 1);  
  88.  
  89.   /* left wall */
  90.   glNormal3f(1, 0, 0);
  91.   glVertex3f(-1, -1, -1);
  92.   glVertex3f(-1, -1, 1);
  93.   glVertex3f(-1, 1, 1);
  94.   glVertex3f(-1, 1, -1);
  95.  
  96.   /* right wall */
  97.   glNormal3f(-1, 0, 0);
  98.   glVertex3f(1, 1, -1);
  99.   glVertex3f(1, 1, 1);
  100.   glVertex3f(1, -1, 1);
  101.   glVertex3f(1, -1, -1);
  102.  
  103.   /* far wall */
  104.   glNormal3f(0, 0, 1);
  105.   glVertex3f(-1, -1, -1);
  106.   glVertex3f(1, -1, -1);
  107.   glVertex3f(1, 1, -1);
  108.   glVertex3f(-1, 1, -1);
  109.  
  110.   glEnd();
  111. }
  112.  
  113. void draw_cone(void)
  114. {
  115.   static GLfloat cone_mat[] = {0.f, .5f, 1.f, 1.f};
  116.  
  117.   glPushMatrix();
  118.   glTranslatef(0, -1, 0);
  119.   glRotatef(-90, 1, 0, 0);
  120.  
  121.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
  122.   gluCylinder(cone, .3, 0, 1.25, 20, 1);
  123.   gluDisk(base, 0., .3, 20, 1); 
  124.  
  125.   glPopMatrix();
  126. }
  127.  
  128. void draw_sphere(GLdouble angle)
  129. {
  130.   static GLfloat sphere_mat[] = {1.f, .5f, 0.f, 1.f};
  131.  
  132.   glPushMatrix();
  133.   glTranslatef(0, -.3, 0);
  134.   glRotatef(angle, 0, 1, 0);
  135.   glTranslatef(0, 0, .6);
  136.  
  137.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
  138.   gluSphere(qsphere, .3, 20, 20);
  139.  
  140.   glPopMatrix();
  141. }
  142.  
  143. GLdouble get_secs(void)
  144. {
  145.   return glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  146. }
  147.  
  148. void draw(void)
  149. {
  150.     GLenum err;
  151.     GLdouble secs;
  152.  
  153.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  154.  
  155.     draw_room();
  156.  
  157.     /* draw the transparent objects... */
  158.     glEnable(GL_POLYGON_STIPPLE);
  159.     glPolygonStipple((GLubyte *)conePattern);
  160.     draw_cone();
  161.  
  162.     glPolygonStipple((GLubyte *)spherePattern);
  163.     secs = get_secs();
  164.     draw_sphere(secs * 360. / 10.);
  165.     glDisable(GL_POLYGON_STIPPLE);
  166.  
  167.     err = glGetError();
  168.     if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));
  169.  
  170.     glutSwapBuffers();
  171. }
  172.  
  173. /* ARGSUSED1 */
  174. void key(unsigned char key, int x, int y)
  175. {
  176.   if (key == 27) exit(0);
  177. }
  178.  
  179. main(int argc, char *argv[])
  180. {
  181.     glutInit(&argc, argv);
  182.     glutInitWindowSize(256, 256);
  183.     glutInitWindowPosition(0, 0);
  184.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  185.     glutCreateWindow(argv[0]);
  186.     glutDisplayFunc(draw);
  187.     glutIdleFunc(draw);
  188.     glutKeyboardFunc(key);
  189.     glutReshapeFunc(reshape);
  190.     init();
  191.  
  192.     glutMainLoop();
  193.     return 0;
  194. }
  195.  
  196.